/* eslint-disable react/no-unused-state */
/* eslint-disable react/sort-comp */
import { Component } from "react";
import { View, Image } from "@tarojs/components";
import Taro from "@tarojs/taro";
import { AtInput, AtForm, AtButton } from "taro-ui";
import "./login.scss";

export default class News extends Component {
  constructor() {
    super(...arguments);
    this.state = {
      user: "",
      psd: "",
      a: ""
    };
  }
  UserChange(value) {
    this.setState({
      user: value
    });
  }
  PsdChange(value) {
    this.setState({
      psd: value
    });
  }
  login() {
    let that = this;
    Taro.request({
      url: "https://dmyg.xyz:3000/login",
      method: "POST",
      data: {
        user: that.state.user,
        psd: that.state.psd
      },
      success: function(res) {
        if (res.data.code == 200) {
          Taro.showToast({
            title: "登录成功",
            icon: "success",
            duration: 2000
          });
          setTimeout(() => {
            Taro.switchTab({
              url: "../index/index"
            });
          }, 1000);
        } else {
          Taro.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          });
          that.setState({
            user: "",
            psd: ""
          });
        }
      }
    });
  }
  componentWillMount() {}

  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}

  render() {
    return (
      <View className="news">
        <AtForm className="formBox">
          <AtInput
            name="user"
            title="用户名"
            type="text"
            placeholder="请输入账号"
            value={this.state.user}
            onChange={this.UserChange.bind(this)}
          />
          <AtInput
            name="psd"
            title="密码"
            type="password"
            placeholder="请输入密码"
            value={this.state.psd}
            onChange={this.PsdChange.bind(this)}
          />

          <AtButton onClick={this.login.bind(this)}>登录</AtButton>
          <AtButton onClick={this.toRegister.bind(this)}>注册</AtButton>
        </AtForm>
      </View>
    );
  }
  // 跳转注册
  toRegister() {
    Taro.redirectTo({
      url: "../register/register"
    });
  }
}
